<template>
	<view class="container">
		<view class="input_box ">
			<view class="title hh">收货人</view>
			<input type="text" v-model="name" placeholder="请输入收货人姓名">
		</view>
		<view class="input_box">
			<view class="title">手机号码</view>
			<input type="number" v-model="mobile" maxlength="11" placeholder="请输入收货人手机号码">
		</view>
		<!-- <view class="input_box" @click="isShwo = true">
			<view class="title">所在地区</view>
			<input type="number" v-model="city" placeholder="请输入收货人所在地区" disabled="">
		</view> -->
		<view class="input_box">
			<view class="title">所在地区</view>
			<input type="text" v-model="dizhi" placeholder="请选择收货人所在地区">
			<view class="icon_box" @click="lookLoaction">
				<u-icon name="map-fill" color="#ea3d37" size="50"></u-icon>
			</view>
		</view>
		<view class="input_box">
			<view class="title">详细地址</view>
			<input type="text" v-model="address" placeholder="请填写详细地址如：楼栋、门牌号等">
		</view>
		<view class="bottom">
			<view class="title">设置默认地址</view>
			<view class="btn_S">
				<view class="text">提醒：每次下单时会使用该地址,实际下单前请确认好地址在下单哦!</view>
				<u-switch v-model="checked" active-color="#EA3D37"></u-switch>
			</view>
		</view>
		<view class="del_btn" @click="delAddress">
			删除地址
		</view>
		<u-modal v-model="show" :content="content" @confirm="confirm" :show-cancel-button="true"></u-modal>
		<u-picker v-model="isShwo" mode="region" @confirm="addConfirm"></u-picker>
		<view class="sebtns" @click="addbank">保存</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				name: '',
				address: '',
				dizhi: '',
				mobile: '',
				city: '',
				checked: false,
				id: null,
				show: false,
				isShwo: false,
				addressObj: {
					province: '',
					province_id: '',
					city: '',
					city_id: '',
					district: '',
					district_id: ''
				}
			}
		},
		onLoad(opdata) {
			if (opdata.isType) {
				this.isType = opdata.isType
			}
			this.getData(opdata.info)
		},
		methods: {
			lookLoaction() {
				uni.chooseLocation({
					success: res => {
						console.log(res);
						console.log('位置名称：' + res.name);
						console.log('详细地址：' + res.address);
						console.log('纬度：' + res.latitude);
						console.log('经度：' + res.longitude);
						this.dizhi = res.address
					}
				});
			},
			getData(aaa) {
				let datas = JSON.parse(aaa)
				this.id = datas.id
				this.$allApi.getAddressInfo(datas.id).then(res => {
					let info = res.data
					this.addressID = datas.id
					this.addressObj = {
						province: info.province,
						province_id: info.province_id,
						city: info.city,
						city_id: info.city_id,
						district: info.district,
						district_id: info.district_id
					}
					this.city = datas.province + '-' + datas.city + '-' + datas.district
					this.address = datas.detail
					this.name = datas.real_name
					this.mobile = datas.phone
					if (datas.is_default == 1) {
						this.checked = true
					} else {
						this.checked = false
					}
				})
				console.log(datas);

			},
			mapClick() {
				uni.chooseLocation({
					success: res => {
						this.city = res.address
					}
				});
			},
			delAddress() {
				this.show = true
			},
			confirm() {
				this.$allApi.delAddress({
					id: this.addressID
				}).then(res => {
					this.$u.toast('删除地址成功')
					setTimeout(() => {
						this.$backTo(1)
					}, 1000)
				})
			},
			addConfirm(e) {
				this.city = e.province.label + '-' + e.city.label + '-' + e.area.label
				this.addressObj.province = e.province.label
				this.addressObj.province_id = e.province.value
				this.addressObj.city = e.city.label
				this.addressObj.city_id = e.city.value
				this.addressObj.district = e.area.label
				this.addressObj.district_id = e.area.value
			},
			addbank() {
				let isDefault = ''
				if (this.checked) {
					isDefault = '1'
				} else {
					isDefault = '0'
				}
				if (this.name == '') {
					this.$u.toast('请输入收货人姓名')
					return
				} else if (this.mobile == '') {
					this.$u.toast('请输入收货人手机号码')
					return
				} else if (this.dizhi == '') {
					this.$u.toast('收货人地区不能为空')
					return
				} else {
					let info = {
						real_name: this.name,
						phone: this.mobile,
						detail: this.dizhi + this.address,
						post_code: '641500',
						id: this.id,
						is_default: isDefault,
						address: this.addressObj
					}
					this.$allApi.editAddress(info).then(res => {
						this.$u.toast('修改地址成功')
						setTimeout(() => {
							this.$backTo(1)
						}, 1500)
					})
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.icon_box {
		width: 100rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.bottom {
		margin-top: 20rpx;
		background: #fff;
		padding: 20rpx;
		color: #666;
		line-height: 1.8;

		.title {
			font-size: 30rpx;
		}

		.btn_S {
			display: flex;
			align-items: center;
			justify-content: space-between;

			.text {
				font-size: 24rpx;
				flex: 1;
				padding-right: 20rpx;
			}
		}
	}

	.del_btn {
		height: 120rpx;
		padding: 20rpx;
		color: #EA3D37;
		background: #fff;
		margin-top: 20rpx;
		display: flex;
		align-items: center;
	}

	.container {
		min-height: 100vh;
		background: #f6f6f6;
	}

	.input_box {
		display: flex;
		align-items: center;
		height: 110rpx;
		border-bottom: 1px solid #eee;
		padding: 20rpx;
		background: #fff;

		.title {}

		.hh {
			letter-spacing: 0.5em;
			/*如果需要y个字两端对齐，则为(x-y)/(y-1),这里是（4-3）/(3-1)=0.5em */
			margin-right: -0.5em;
			/*同上*/
		}

		input {
			flex: 1;
			padding-left: 30rpx;
		}
	}

	.sebtns {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100vw;
		background: #EA3D37;
		color: #fff;
		font-size: 32rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		border-radius: 35rpx;
		height: 70rpx;
		bottom: calc(20rpx + constant(safe-area-inset-bottom));
		bottom: calc(20rpx + env(safe-area-inset-bottom));
	}
</style>